package com.example.service.impl;

import com.example.mapper.ArticleCategoryMapper;
import com.example.mapper.ArticlesMapper;
import com.example.mapper.AuthorMapper;
import com.example.pojo.*;
import com.example.service.ArticlesService;
import com.example.service.AuthorService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ArticlesServicelmpl implements ArticlesService {

    @Autowired
    private ArticlesMapper articlesMapper;
    @Autowired
    private AuthorMapper authorMapper;
    @Autowired
    private ArticleCategoryMapper  articleCategoryMapper;
    @Override
    public PageResult<Articles> page(ArticleQueryParam studentQueryParam) {
        PageHelper.startPage(studentQueryParam.getPage(),studentQueryParam.getPageSize());
        List<Articles> clazzList= articlesMapper.list(studentQueryParam);
        Page<Articles> p= (Page<Articles>)clazzList;
        return new PageResult<Articles>(p.getTotal(),p.getResult());
    }

    @Override
    public void Add(Articles student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        Author a=authorMapper.getById(student.getAuthorId());
        student.setAuthor(a.getName());
        ArticleCategory a1=articleCategoryMapper.getById(student.getCategoryId());
        student.setCategoryName( a1.getName());
        articlesMapper.add(student);
    }

    @Override
    public Articles GetById(Integer id) {
        return articlesMapper.getbyid(id);
    }

    @Override
    public void update(Articles student) {
        student.setUpdateTime(LocalDateTime.now());
        Author a=authorMapper.getById(student.getAuthorId());
        student.setAuthor(a.getName());
        ArticleCategory a1=articleCategoryMapper.getById(student.getCategoryId());
        student.setCategoryName( a1.getName());
        articlesMapper.updateclazz(student);
    }

    @Override
    public void delete(List<Integer> ids) {
        articlesMapper.deleteByIds(ids);

    }

    @Override
    public void updataById(Integer id, Integer score) {
        articlesMapper.updata(id,score);
    }

    @Override
    public PageResult<Articles> page2(ArticleQueryParam studentQueryParam) {
        PageHelper.startPage(studentQueryParam.getPage(),studentQueryParam.getPageSize());
        List<Articles> clazzList= articlesMapper.list2(studentQueryParam);
        Page<Articles> p= (Page<Articles>)clazzList;
        return new PageResult<Articles>(p.getTotal(),p.getResult());
    }
}
